package com.qin.mall.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.domain.PmsProduct;
import com.qin.mall.service.PmsProductService;
import com.qin.mall.dto.PmsProductParam;
import com.qin.mall.dto.PmsProductQueryParam;
import com.qin.mall.dto.PmsProductResult;
import com.qin.mall.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品管理
 * @author Leisure
 * create 2022 2022/11/7 15:17
 **/
@RestController
@RequestMapping("/product")
public class ProductController {
    
    @Autowired
    private PmsProductService productService;

    /**
     * 测试数据先从select 中取到 再进来测试可以通过
     * @param productParam
     * @return
     */
    // 创建商品
    @PostMapping("/create$")
    public CommonResult create(@RequestBody PmsProductParam productParam){
        int count = productService.create(productParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    

    //  查询商品信息不关联其他信息
    @GetMapping("select$")
    public Object select(@RequestParam("id") Long id) throws JsonProcessingException {
        PmsProduct product = productService.select(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(product);
        if (result!=null){
            return result;
        }
        else {
            return ResultCode.FAILED;
        }
    }

    // TODO 根据商品编号获取更新前的信息,和其他关联的所有的信息
    @GetMapping("/updateInfo$/{id}")
    public CommonResult<PmsProductResult> getUpdateInfo(@PathVariable Long id) throws JsonProcessingException {
        PmsProductResult product = productService.getUpdateInfo(id);
        return CommonResult.success(product);
    }

    // TODO 更新商品
    @PostMapping("/update$/")
    public CommonResult updateById(@RequestParam("id") Long id,
                             @RequestBody PmsProductParam productParam){
        int count = productService.update(id, productParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    // TODO 分页查询商品
    @PostMapping("/list$")
    public CommonResult<MyBatisPlusPage<PmsProduct>> getListAll(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSiz,
            @RequestBody PmsProductQueryParam productQueryParam){
        Page<PmsProduct> list = productService.getList(productQueryParam, pageNum, pageSiz);
        return CommonResult.success(MyBatisPlusPage.mpPage(list));
        
    }

    // 批量修改审核状态
    @PostMapping("/update/verifyStatus$")
    public CommonResult updateStatus(@RequestParam("verifyStatus") Integer verifyStatus,
                               @RequestParam("detail") String detail,
                               @RequestBody List<Long> ids) {
        int count = productService.updateVerifyStatus(ids, verifyStatus, detail);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }

    }
    
    //批量修改商品上下架状态
    @PostMapping("/update/publishStatus$")
    @ResponseBody
    public CommonResult updatepublishStatus(@RequestParam("publishStatus") Integer publishStatus,
                                            @RequestParam("ids") List<Long> ids){
        int count = productService.updatePublishStatus(ids, publishStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    
    //批量修改商品推荐状态
    @PostMapping("/update/recommendStatus$")
    @ResponseBody
    public CommonResult updateRecommendStatus(@RequestParam("recommendStatus") Integer recommendStatus,
                                              @RequestParam("ids") List<Long> ids){
        int count = productService.updateRecommendStatus(ids, recommendStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    
    //批量修改新品状态
    @PostMapping("/update/newStatus$")
    @ResponseBody
    public CommonResult updateNewStatus(@RequestParam("newStatus") Integer newStatus,
                                        @RequestParam("ids") List<Long> ids){
        int count = productService.updateNewStatus(ids, newStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //批量删除商品
    @PostMapping("/update/deleteStatus$")
    public CommonResult updateDeleteStatus(@RequestParam("deleteStatus") Integer deleteStatus,
                                           @RequestParam("ids") List<Long> ids){
        int count = productService.updateDeleteStatus(ids, deleteStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据商品名称或者货号模糊查询
     * @param keyword
     * @return
     */
    @GetMapping("/simpleList$")
    public CommonResult<List<PmsProduct>> getList(@RequestParam("keyword") String keyword){
        List<PmsProduct> info = productService.list(keyword);
        return CommonResult.success(info);
    }
}
